package com.qili.controller;

import com.qili.entity.base.BaseCompany;
import com.qili.entity.supplier.RecommendReason;
import com.qili.entity.supplier.SupplierProject;
import com.qili.entity.supplier.SupplierProjectAccept;
import com.qili.entity.sys.SysUser;
import com.qili.service.SysUserService;
import com.qili.service.base.BaseCompanyService;
import com.qili.service.supplier.ProjectCollectService;
import com.qili.service.supplier.RecommendReasonService;
import com.qili.service.supplier.SupplierProjectAcceptService;
import com.qili.service.supplier.SupplierProjectManagerService;
import com.qili.util.DateUtil;
import com.qili.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangxiaopeng
 * @date 2021-04-12
 * @description 综合对比
 */
@Controller
@RequestMapping("/recommend")
public class RecommendReasonController {

    @Autowired
    ProjectCollectService pcService;
    @Autowired
    RecommendReasonService reService;
    @Autowired
    SupplierProjectManagerService supplierProjectManagerService;
    @Autowired
    SupplierProjectAcceptService supplierProjectAcceptService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    BaseCompanyService baseCompanyService;
    /**
     *综合对比跳转
     * @param supProjectId
     * @return
     */
    @RequestMapping("comparison")
    public String comparison(Model model,String supProjectId){
        RecommendReason reason=new RecommendReason();
        reason.setProjectId(supProjectId);
        List<RecommendReason> reasonList= recommend(reason);
        model.addAttribute("reasonList",reasonList);
        return "projectManager/comprehensiveComparison";
    }

    //查询项目是否到截止日期
    @RequestMapping("time")
    @ResponseBody
    public JsonUtil time(String id){
        JsonUtil json=new JsonUtil();
       SupplierProject sup= supplierProjectManagerService.selectByPrimaryKey(id);
        Date zbEndDate = DateUtil.parseDate(sup.getZbEndDate(), "yyyy-MM-dd HH:mm");
        Date currdate = new Date();
        if (currdate.after(zbEndDate)) {
            json.setFlag(true);
        }else{
            json.setFlag(false);
        }
        return json;
    }


    /**
     * 查询综合对数据
     * @param reason
     * @return
     */
    public List<RecommendReason> recommend(RecommendReason reason){
        Map map=new HashMap<>();
        map.put("projectId",reason.getProjectId());
        map.put("acceptDesc","123");
        List<RecommendReason> recommendReason= reService.selectReason(map);
        SysUser sysUser =new SysUser();
        SupplierProjectAccept spa=new SupplierProjectAccept();
        String name="";
        //判断是个人还是组织
        for(RecommendReason reason1 :recommendReason){
            if("001".equals(reason1.getComType())){
                spa=supplierProjectAcceptService.selectByPrimaryKey(reason1.getAcceptId());
                sysUser = sysUserService.selectByPrimaryKey(spa.getAcceptBy());
                if("001".equals(sysUser)){
                    name=sysUser.getRealName();
                }else{
                    if(StringUtils.isNotBlank(sysUser.getRealName())){
                        name=sysUser.getRealName();
                    }else{
                        name=sysUser.getLinkPerson();
                    }
                }
                BaseCompany baseCompany = baseCompanyService.selectByPrimaryKey(reason1.getCompanyId());
                reason1.setRecommendName(baseCompany.getBcName()+"("+name+")");
            }else{
                sysUser = sysUserService.selectByPrimaryKey(reason1.getCompanyId());
                if("001".equals(sysUser)){
                    name=sysUser.getRealName();
                }else{
                    if(StringUtils.isNotBlank(sysUser.getRealName())){
                        name=sysUser.getRealName();
                    }else{
                        name=sysUser.getLinkPerson();
                    }
                }
                reason1.setRecommendName(sysUser.getCompanyName()+"("+name+")");
            }
        }
        return recommendReason;
    }
    

}
